﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabMonitorRemote
{
    public partial class TestCommunicationUserControl : JunkControl
    {
        DewkPing _DewkPing;

        public TestCommunicationUserControl(DewkPing dewk)
        {
            InitializeComponent();
            _DewkPing = dewk;
            SubscribeToPing();
            InitializeLabels();           
        }

        private void SubscribeToPing()
        {
            _DewkPing.ConnectionDetermined += new DewkPing.ConnectionDeterminedEventHandler(DisplayPingResult);
        }

        private void DisplayPingResult(object sender, EventArgs e)
        {
            if (((DewkPing)sender).IsConnected)
            {
                this.Invoke((Action)delegate
                {
                    lblIpAddress.Text = ((DewkPing)sender).IpAddress;
                    lblModelNumber.Text = String.Format("Model: {0}, S/N: {1}", ((DewkPing)sender).ModelNumber, ((DewkPing)sender).SerialNumber);
                    lblFirmwareVersion.Text = String.Format("Firmware Version: {0}", ((DewkPing)sender).FirmwareVersion);
                    lblConnectionStatus.Text = "CONNECTED";
                    lblConnectionStatus.ForeColor = Color.Green;
                    lblLastTested.Text = String.Format("Last Tested: {0}", DateTime.Now.ToShortTimeString());

                    lblModelNumber.Visible = true;
                    lblFirmwareVersion.Visible = true;
                    lblConnectionStatus.Visible = true;
                    lblLastTested.Visible = true;
                });
            }
            else
            {
                this.Invoke((Action)delegate
                {
                    lblIpAddress.Text = ((DewkPing)sender).IpAddress;
                    lblConnectionStatus.Text = "NOT CONNECTED";
                    lblConnectionStatus.ForeColor = Color.Red;
                    lblLastTested.Text = String.Format("Last Tested: {0}", DateTime.Now.ToShortTimeString());

                    lblModelNumber.Visible = false;
                    lblFirmwareVersion.Visible = false;
                    lblConnectionStatus.Visible = true;
                    lblLastTested.Visible = true;
                });
            }
        }

        private void InitializeLabels()
        {
            lblLoggerName.Text = _DewkPing.LoggerName;
            lblIpAddress.Text = _DewkPing.IpAddress;

            lblModelNumber.Visible = false;
            lblFirmwareVersion.Visible = false;
            lblConnectionStatus.Visible = false;
            lblLastTested.Visible = false;
        }
        
        public override Boolean LoggerCommunicationActive()
        {
            return false;
        }
         
    }
}
